//
// Created by HC_yyt on 2021/7/22/022.
//

#include "btmpHandler.h"

/***
 * c语言环境mat 转为 bitmap
 * @param env
 * @param src
 * @param needPremultiplyAlpha
 * @param bitmap_config
 * @return bitmap
 */
jobject mat2bitmap(JNIEnv *env, cv::Mat &src, bool needPremultiplyAlpha, jobject bitmap_config) {
    jclass java_bitmap_class = (jclass) env->FindClass("android/graphics/Bitmap");
    jmethodID mid = env->GetStaticMethodID(java_bitmap_class, "createBitmap","(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
    jobject bitmap = env->CallStaticObjectMethod(java_bitmap_class,
                                                 mid, src.size().width, src.size().height,
                                                 bitmap_config);
    AndroidBitmapInfo info;
    void *pixels = 0;

    try {
        CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0);
        CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4);
        CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0);
        CV_Assert(pixels);

        if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
            cv::Mat tmp(info.height, info.width, CV_8UC4, pixels);
            if (src.type() == CV_8UC1) {
                cvtColor(src, tmp, cv::COLOR_GRAY2RGBA);
            } else if (src.type() == CV_8UC3) {
                cvtColor(src, tmp, cv::COLOR_RGB2BGRA);
            } else if (src.type() == CV_8UC4) {
                if (needPremultiplyAlpha) {
                    cvtColor(src, tmp, cv::COLOR_RGBA2mRGBA);
                } else {
                    src.copyTo(tmp);
                }
            }
        } else {
            // info.format == ANDROID_BITMAP_FORMAT_RGB_565
            cv::Mat tmp(info.height, info.width, CV_8UC2, pixels);
            if (src.type() == CV_8UC1) {
                cvtColor(src, tmp, cv::COLOR_GRAY2BGR565);
            } else if (src.type() == CV_8UC3) {
                cvtColor(src, tmp, cv::COLOR_RGB2BGR565);
            } else if (src.type() == CV_8UC4) {
                cvtColor(src, tmp, cv::COLOR_RGBA2BGR565);
            }
        }
        AndroidBitmap_unlockPixels(env, bitmap);
        return bitmap;
    } catch (cv::Exception e) {
        AndroidBitmap_unlockPixels(env, bitmap);
        LOGEI("mat2bitmap error %s",e.msg.c_str());
        return bitmap;
    } catch (...) {
        AndroidBitmap_unlockPixels(env, bitmap);
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "Unknown exception in JNI code {nMatToBitmap}");
        return bitmap;
    }
}

/**
 * 将bitmp 转为灰度图
 * @param env
 * @param thiz
 * @param bitmap
 * @return bitmap
 */
jobject bmp2grayhandler(JNIEnv *env, jobject thiz, jobject bitmap){
    AndroidBitmapInfo bitmapInfo;
    void *pixelscolor;
    int ret;
    if((ret = AndroidBitmap_getInfo(env,bitmap,&bitmapInfo)) <0){
        LOGEI("Bitmp_getInfo error %d",ret);
        return NULL;
    }
    if(bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888){
        LOGEI("BitmpInfoFormat error!");
        return NULL;
    }
    if((ret = AndroidBitmap_lockPixels(env,bitmap,&pixelscolor)) < 0){
        LOGEI("Bitmp_lockPixels failed error %d",ret);
        return NULL;
    }
    cv::Mat src(bitmapInfo.height,bitmapInfo.width,CV_8UC4,pixelscolor);
    cv::cvtColor(src,src,cv::COLOR_RGBA2GRAY);
    jclass java_bitmap_class = (jclass) env->FindClass("android/graphics/Bitmap");

    jmethodID mid = env->GetMethodID(java_bitmap_class, "getConfig","()Landroid/graphics/Bitmap$Config;");
    jobject bitmap_config = env->CallObjectMethod(bitmap, mid);
    //将SRC转换为图片
    jobject _bitmap = mat2bitmap(env, src, false, bitmap_config);
    AndroidBitmap_unlockPixels(env, bitmap);
    return _bitmap;
}
